﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AccManagerKw.Common;
using DataAccess;
using Model;
using Model.Interfaces;
using Utilities;

namespace AccManagerKw.Masters
{
    public partial class FrmStoreMaster : FrmBaseMaster, ICrud
    {

        private ClsCommonFn _oCommFn = new ClsCommonFn();
        private StoreMaster _row = new StoreMaster();
        private ClsDataFn _oDataFn = new ClsDataFn();
        public FrmStoreMaster()
        {
            InitializeComponent();
        }
        private void FrmStoreMaster_Shown(object sender, EventArgs e)
        {
            try
            {
                _oCommFn.GroupControl_Setting(gclMain);
                _oCommFn.GroupControl_Setting(gclOpen);
                _oDataFn.SetDataSource(ClsGlobalEnums.EDataSourceType.Cities, lucCities, "CityName", "CityId");
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
            }
        }


        public bool OperationSave()
        {
            try
            {
                if (IsValid())
                {
                    if (Mode == ClsGlobalVarz.Mode.Add && _oCommFn.ValidateCrud(Mode))
                    {
                        OperationStore();
                        _oCommFn.MsgBoxCrud(Mode);
                        return true;
                    }
                    else if (Mode == ClsGlobalVarz.Mode.Edit && _oCommFn.ValidateCrud(Mode))
                    {
                        OperationStore();
                        _oCommFn.MsgBoxCrud(Mode);
                        return true;
                    }
                    else if (Mode != ClsGlobalVarz.Mode.Add && Mode != ClsGlobalVarz.Mode.Edit)
                    {
                        _oCommFn.WarningMsg("Check Mode");
                        return false;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
                return false;
            }
        }
        private void OperationStore()
        {
            using (var context = new AccManagerEntities())
            {
                var oStoreMaster = new StoreMaster();
                if (Mode == ClsGlobalVarz.Mode.Add)
                {
                    oStoreMaster.StoreId = context.StoreMasters.ToList().Count > 0
                        ? context.StoreMasters.Max(x => x.StoreId) + 1 : 1;
                }
                else if (Mode == ClsGlobalVarz.Mode.Edit)
                {
                    oStoreMaster = context.StoreMasters.Single(x => x.StoreId == _row.StoreId);
                }

                oStoreMaster.StoreName = txtStoreName.Text;
                oStoreMaster.Location = txtLocation.Text;
                oStoreMaster.CityId = lucCities.Value;

                if (Mode == ClsGlobalVarz.Mode.Add)
                    context.StoreMasters.Add(oStoreMaster);
                context.SaveChanges();
            }
        }


        public void OperationOpen()
        {
            var context = new AccManagerEntities();
            gcStores.DataSource = context.StoreMasters.ToList();
            gclOpen.BringToFront();
        }
        private void gvStores_DoubleClick(object sender, EventArgs e)
        {
            _row = (StoreMaster)gvStores.GetFocusedRow();
            txtStoreName.Text = _row.StoreName;
            txtLocation.Text = _row.Location;
            lucCities.EditValue = _row.CityId;

            _oCommFn.DisableControls(gclMain, true);
            gclMain.BringToFront();
        }


        public void OperationEdit()
        {
            if (_oCommFn.ValidateOpenRecord(_row.StoreId))
            {
                _oCommFn.DisableControls(gclMain, false);
                SetMode(ClsGlobalVarz.Mode.Edit);
            }
        }

        public bool OperationDelete()
        {
            try
            {
                if (_oCommFn.ValidateOpenRecord(_row.StoreId))
                {
                    SetMode(ClsGlobalVarz.Mode.Delete);
                    if (_oCommFn.ValidateCrud(Mode))
                    {
                        using (var context = new AccManagerEntities())
                        {
                            var oStoreMaster = context.StoreMasters.Single(x => x.StoreId == _row.StoreId);
                            context.StoreMasters.Remove(oStoreMaster);
                            context.SaveChanges();
                            _oCommFn.MsgBoxCrud(Mode);
                        }
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
                return false;
            }
        }

        public void ResetData()
        {
            _oCommFn.ClearFields(this);
            _oCommFn.DisableControls(gclMain, false);
            gclMain.BringToFront();
            SetMode(ClsGlobalVarz.Mode.Add);
        }

        public bool IsValid()
        {
            ccDxValidationProvider1.Validate();
            return ccDxValidationProvider1.GetInvalidControls().Count <= 0;
        }




    }
}
